package com.anysoft.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anysoft.HostConstant;
import com.anysoft.R;
import com.anysoft.adapter.FundListNewAgainAdapter;
import com.anysoft.base.BaseFragment;
import com.anysoft.bean.BannersBean;
import com.anysoft.bean.ChenkUserAndFundLevelBean;
import com.anysoft.bean.QueryFundListBean;
import com.anysoft.bean.QuickLevelBean;
import com.anysoft.event.FundBannerLoginEvent;
import com.anysoft.fund.FundListAllActivity;
import com.anysoft.guide.GuideAdapter;
import com.anysoft.home.HomeActivity;
import com.anysoft.utils.AESUtils;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.GlideImageLoader;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.MentorLogger;
import com.anysoft.utils.StringUtils;
import com.anysoft.view.CustomMarqueeTextView;
import com.anysoft.view.PullToRefreshView;
import com.anysoft.view.dialog.IcbcDialog;
import com.anysoft.web.AssetsFundBuyActivity;
import com.anysoft.web.BannerUrlActivity;
import com.anysoft.web.FundDetailActivity;
import com.anysoft.web.FundIncomeRankingActivity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.anysoft.HostConstant.HOST_WEB_URL_HEADER;
import static com.anysoft.utils.user.UserBeanUtil.hasUser;
import static org.greenrobot.eventbus.EventBus.TAG;

/**
 * Created by wwq on 2017/2/23.
 * 基金页面
 */

public class FundNewAgainTab extends BaseFragment implements PullToRefreshView.OnHeaderRefreshListener, View.OnClickListener {
    
    @BindView(R.id.leftButton)
    Button leftButton;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rightButton)
    Button rightButton;
    @BindView(R.id.titlebar)
    FrameLayout titlebar;
    @BindView(R.id.lv_fund)
    ListView lvFund;
    @BindView(R.id.pullToRefreshView)
    PullToRefreshView pullToRefreshView;
    
    RelativeLayout rl_vp_fund_home;
    LinearLayout ll_fund_selected;
    LinearLayout llFundHomeAll;
    LinearLayout llFundHomeMyFocus;
    LinearLayout llFundHomeIncomeRanking;
    @BindView(R.id.iv_leftButton)
    ImageView ivLeftButton;
    @BindView(R.id.cmtv_title)
    CustomMarqueeTextView cmtvTitle;
    Banner vp_home_banner;
    
    private String json;
    
    private FundListNewAgainAdapter fundAdapter;
    //    private FundListNewAdapter fundAdapter;
    private List<QueryFundListBean.DataBean> selectedList = new ArrayList<>(); //精选基金
    private List<QueryFundListBean.DataBean> fundNewList = new ArrayList<>(); //新发基金
    
    private boolean isFundLoadSuccess = false;
    private boolean isBannerLoadSuccess = false;
    
    private boolean isFundRefreshSuccess = false;
    private boolean isBannerRefreshSuccess = false;
    
    protected View headerView;
    private BannersBean.DataBean bannersBean;
    private boolean isCacheFundList = false;
    private boolean isCacheBanners = false;
    private HomeActivity activity;
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (HomeActivity) context;
    }
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fund_new, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        leftButton.setVisibility(View.GONE);
        ivLeftButton.setVisibility(View.GONE);
        title.setText("基金");
        
        pullToRefreshView.setOnHeaderRefreshListener(this);
        pullToRefreshView.setIsCancleLoadMore();
        
        headerView = View.inflate(getActivity(), R.layout.header_fund, null);
        rl_vp_fund_home = (RelativeLayout) headerView.findViewById(R.id.rl_vp_fund_home);
        ll_fund_selected = (LinearLayout) headerView.findViewById(R.id.ll_fund_selected);
        vp_home_banner = (Banner) headerView.findViewById(R.id.vp_home_banner);
        llFundHomeAll = (LinearLayout) headerView.findViewById(R.id.ll_fund_home_all);
        llFundHomeMyFocus = (LinearLayout) headerView.findViewById(R.id.ll_fund_home_my_focus);
        llFundHomeIncomeRanking = (LinearLayout) headerView.findViewById(R.id.ll_fund_home_income_ranking);
        
        setBannerContainerScale();
        llFundHomeAll.setOnClickListener(this);
        llFundHomeMyFocus.setOnClickListener(this);
        llFundHomeIncomeRanking.setOnClickListener(this);
        
        lvFund.addHeaderView(headerView);
        
        getBanners();
        getInfos();
        return view;
    }
    
    private void setBannerContainerScale() {
        ViewGroup.LayoutParams params = rl_vp_fund_home.getLayoutParams();
        int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
        params.height = (int) (screenWidth / 2.7);
        rl_vp_fund_home.setLayoutParams(params);
    }
    
    @Override
    public void onResume() {
        super.onResume();
    }
    
    @Override
    public void onPause() {
        super.onPause();
    }
    
    /**
     * 获取基金列表
     */
    private void getInfos() {
        OkGo.<String>post(HostConstant.QUERY_FUNDLIST)
                .tag(this)
                .params(HttpUtils.getCommonParams(activity, null))
                .cacheKey(AESUtils.encrypt("isCacheFundList", HostConstant.QUERY_FUNDLIST))
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        json = response.body();
                        UpdateUI(response.body());
                        isFundRefreshSuccess = true;
                        refreshComplete();
                    }
                    
                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        if (!isCacheFundList) {
                            //一般来说,缓存回调成功和网络回调成功做的事情是一样的,所以这里直接回调onSuccess
                            onSuccess(response);
                            isCacheFundList = true;
                        }
                    }
                    
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        isFundRefreshSuccess = true;
                        refreshComplete();
                    }
                    
                    @Override
                    public void onFinish() {
                        refreshComplete();
                    }
                });
    }
    
    private void UpdateUI(String resultJson) {
        fundNewList.clear();
        selectedList.clear();
        
        QueryFundListBean bean;
        try {
            bean = new Gson().fromJson(resultJson, QueryFundListBean.class);
        } catch (Exception e) {
            Logger.e("FundTab", "Gson解析数据时发生异常");
            ll_fund_selected.setVisibility(View.GONE);
            e.printStackTrace();
            return;
        }
        
        if (!CommonUtil.checkState(activity, bean, resultJson)) {
            ll_fund_selected.setVisibility(View.GONE);
            return;
        }
        
        if (bean == null || bean.getData() == null || bean.getData().isEmpty() || bean.getData().size() < 1) {
//            showConfirmAlert(activity, "暂无推荐和新发基金");
            ll_fund_selected.setVisibility(View.GONE);
            return;
        }
        
        isFundLoadSuccess = true;
        
        for (QueryFundListBean.DataBean DataBean : bean.getData()) {
            if ("1".equals(DataBean.getIsNewFund())) {
                fundNewList.add(DataBean);
            }
            
            if ("1".equals(DataBean.getIsRecommend())) {
                if (selectedList.size() < 5) {
                    selectedList.add(DataBean);
                }
            }
        }
        
        if (selectedList.size() > 0) {
            ll_fund_selected.setVisibility(View.VISIBLE);
        }
        
        fundAdapter = new FundListNewAgainAdapter(getActivity(), selectedList, fundNewList);
        lvFund.setAdapter(fundAdapter);
        
        fundAdapter.setOnBuyQuicklyBtnListener(new FundListNewAgainAdapter.OnBuyQuicklyBtnListener() {
            @Override
            public void OnBuyQuicklyBtnClick(QueryFundListBean.DataBean bean) {
                if (activity.mentorApplication.needLogin()) {
                    ActivityUtils.gotoLogin(getActivity(), activity.hasUser(), false, activity.findUserId());
                } else {
                    quickLevel(bean);
                }
            }
        });
        
        fundAdapter.setOnBuyBtnListener(new FundListNewAgainAdapter.OnBuyBtnListener() {
            @Override
            public void OnBuyBtnClick(QueryFundListBean.DataBean bean) {
                if (activity.mentorApplication.needLogin()) {
                    ActivityUtils.gotoLogin(getActivity(), activity.hasUser(), false, activity.findUserId());
                } else {
                    quickLevel(bean);
                }
            }
        });
        
        fundAdapter.setOnRelativeLayoutListener(new FundListNewAgainAdapter.OnRelativeLayoutClickListener() {
            @Override
            public void OnRelativeLayoutClick(QueryFundListBean.DataBean bean) {
                Intent intent = new Intent().setClass(activity, FundDetailActivity.class);
                intent.putExtra("fundcode", bean.getFundcode());
                intent.putExtra("fundname", bean.getFundname());
                intent.putExtra("fundType", bean.getFundtype());
                intent.putExtra("mainTitle", bean.getFundname() + " " + bean.getFundcode());
                intent.putExtra("subTitle", bean.getFundstateDescription());
                startActivity(intent);
            }
        });
    }
    
    /**
     * 比较客户风险等级和基金风险等级
     */
    private void quickLevel(final QueryFundListBean.DataBean bean) {
        activity.showLoading(activity);
        Map<String, String> params = new HashMap<>();
        params.put("fundCustomerNo", activity.getFundCustomerNo());
        params.put("fundCode", bean.getFundcode());
        params.put("doType", "比较");
        OkGo.<String>post(HostConstant.QUICK_LEVEL)
                .tag(this)
                .params(HttpUtils.getCommonParams(activity, params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        UpdateQuickLevel(response.body(), bean);
                    }
                    
                    
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        activity.showToast("网络加载失败，请稍后再试!");
                        activity.hideLoading();
                    }
                    
                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }
    
    private void UpdateQuickLevel(String json, final QueryFundListBean.DataBean dataBean) {
        activity.hideLoading();
        QuickLevelBean bean;
        try {
            bean = new Gson().fromJson(json, QuickLevelBean.class);
        } catch (Exception exception) {
            activity.showConfirmAlert(getActivity(), "提示", "数据解析错误");
            return;
        }
        
        if (!CommonUtil.checkState(activity, bean, json)) {
            return;
        }
        
        if (bean == null || bean.getData() == null || TextUtils.isEmpty(bean.getData().getType())) {
            activity.showConfirmAlert(getActivity(), "提示", getString(R.string.no_data));
            return;
        }
        
        IcbcDialog dialog = new IcbcDialog(getActivity());
        
        switch (Integer.parseInt(bean.getData().getType())) {
            case 0:
                dialog.setAnimationEnable(true);
                dialog.setCancelable(false);
                dialog.setTitle("风险提示");
                dialog.setContentText("您还没有做风险测评，不能进行交易。");
                dialog.setEditTextType(false);
                dialog.setNegativeListener("取消", new IcbcDialog.OnNegativeListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                    }
                });
                dialog.setPositiveListener("风险测评", new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        activity.startRiskActivity(null);
                    }
                }).show();
                break;
            case 1:
                dialog.setAnimationEnable(true);
                dialog.setCancelable(false);
                dialog.setTitle("风险提示");
//                dialog.setContentText("该基金产品与您的风险不匹配，不允许购买。如有疑问请联系客服。");
                //TODO XXXX风险级别的基金产品
                dialog.setContentText("您选择的基金产品的风险等级（" + dataBean.getFundrisklevelName() + "）超过了您的风险承受能力（安全型）。" +
                        "投资该基金可能产生的亏损有可能超过您的承受范围，根据有关法律文件的规定，建议您选择其它低风险的产品进行购买。");
//                dialog.setContentText("您的风险承受力为安全型，不能购买" + dataBean.getFundrisklevelName() + "风险级别的基金产品，有疑问请联系客服。");
                dialog.setEditTextType(false);
//                dialog.setNegativeListener("取消", new IcbcDialog.OnNegativeListener() {
//                    @Override
//                    public void onClick(IcbcDialog dialog) {
//                        dialog.dismiss();
//                    }
//                });
                dialog.setPositiveListener("确定", new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
//                        activity.startRiskActivity(activity.mentorApplication.accountLoginBean.getData().getLevel());
                    }
                }).show();
                break;
            case 2:
                dialog.setAnimationEnable(true);
                dialog.setCancelable(false);
                dialog.setTitle("风险提示");
                dialog.setContentText("您选择的基金产品的风险等级（" + dataBean.getFundrisklevelName() +
                        "）超过了您的风险承受能力（" + activity.mentorApplication.accountLoginBean.getData().getLevelName() + "）。" +
                        "投资该基金可能产生的亏损有可能超过您的承受范围，建议您选择适合您" +
                        "风险承受能力的基金产品进行购买。如您已知晓相关风险，确认购买该基金的行为" +
                        "出自本人真实意愿，请点击“确定”表示同意继续购买。 ");
//                dialog.setContentText("该产品超过了您的风险承受能力，是否继续交易？");
                dialog.setEditTextType(false);
                dialog.setNegativeListener("取消", new IcbcDialog.OnNegativeListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                    }
                });
                dialog.setPositiveListener("确定", new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        chenkUserAndFundLevel(dataBean);
                    }
                }).show();
                break;
            case 3:
                gotoBuy(dataBean);
                break;
            default:
                break;
        }
    }
    
    /**
     * 查询客户风险等级和基金风险等级的比较留痕用
     */
    private void chenkUserAndFundLevel(final QueryFundListBean.DataBean bean) {
        Map<String, String> params = new HashMap<>();
        params.put("fundCustomerNo", activity.getFundCustomerNo());
        params.put("fundCode", bean.getFundcode());
        params.put("doType", "认申购");
        OkGo.<String>post(HostConstant.CHENK_USER_AND_FUND_LEVEL)
                .tag(this)
                .params(HttpUtils.getCommonParams(activity, params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        MentorLogger.LogJson(TAG + "查询客户风险等级和基金风险等级的比较留痕用:", response.body());
                        UpdateChenkUserAndFundLevel(response.body(), bean);
                    }
                    
                    
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        activity.showToast("网络加载失败，请稍后再试!");
                    }
                    
                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }
    
    private void UpdateChenkUserAndFundLevel(String json, final QueryFundListBean.DataBean dataBean) {
        
        ChenkUserAndFundLevelBean bean;
        try {
            bean = new Gson().fromJson(json, ChenkUserAndFundLevelBean.class);
        } catch (Exception exception) {
            activity.showConfirmAlert(getActivity(), "提示", "数据解析错误");
            return;
        }
        
        if (!CommonUtil.checkState(activity, bean, json)) {
            return;
        }
        
        gotoBuy(dataBean);
    }
    
    private void gotoBuy(QueryFundListBean.DataBean bean) {
        String url = HOST_WEB_URL_HEADER + ".fundBuy.html?isFromApp=true&" + HttpUtils.getCommonParams2StringByWeb(activity)
                + "&cusNo=" + activity.getFundCustomerNo() + "&fundCode=" + bean.getFundcode()
                + "&fundstateid=" + bean.getFundstateid() + "&fundrisklevel=" + bean.getFundrisklevel()
                + "&shareClass=" + bean.getShareClass() + "&fundName=" + bean.getFundname();
        
        Intent mIntent = new Intent().setClass(getActivity(), AssetsFundBuyActivity.class);
        String param = url.split("\\?")[1];
        mIntent.putExtra("mainTitle", bean.getFundname() + " " + bean.getFundcode());
        mIntent.putExtra("param", param);
        startActivity(mIntent);
    }
    
    private List<BannersBean.DataBean> pageList = new ArrayList<>();
    private List<View> viewList = new ArrayList<>();
    private GuideAdapter adapter;
    
    private void getBanners() {
        Map<String, String> params = new HashMap<>();
        params.put("bannerType", "3");
        OkGo.<String>post(HostConstant.LIST_BANNERS)
                .tag(this)
                .params(HttpUtils.getCommonParams(activity, params))
                .cacheKey(AESUtils.encrypt("isCacheBanners", HostConstant.LIST_BANNERS))
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        json = response.body().toString();
                        bannersUpdateUI(response.body().toString());
                        
                        isBannerRefreshSuccess = true;
                        refreshComplete();
                    }
                    
                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        if (!isCacheBanners) {
                            onSuccess(response);
                            isCacheBanners = true;
                        }
                    }
                    
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        isBannerRefreshSuccess = true;
                        activity.showToast("网络加载失败，请稍后再试!");
                        refreshComplete();
                    }
                    
                    @Override
                    public void onFinish() {
                        refreshComplete();
                    }
                });
        
    }
    
    private void bannersUpdateUI(String resultJson) {
        pageList.clear();
        viewList.clear();
        
        BannersBean bean;
        try {
            bean = new Gson().fromJson(resultJson, BannersBean.class);
        } catch (Exception e) {
            Logger.e("FundTab", "Gson解析数据时发生异常");
            e.printStackTrace();
            return;
        }
        
        if (!CommonUtil.checkState(activity, bean, resultJson)) {
            return;
        }
        
        if (bean.getData() == null || bean.getData().isEmpty()) {
            return;
        }
        
        rl_vp_fund_home.setVisibility(View.VISIBLE);
        isBannerLoadSuccess = true;
        pageList.addAll(bean.getData());
        initBannersData();
        
    }
    
    private void initBannersData() {
        String bannerTime = pageList.get(0).getBannerIntervalTime();
        int bannerIntervalTime = 0;
        if (TextUtils.isEmpty(bannerTime)) {
            bannerIntervalTime = 5000;
        } else {
            bannerIntervalTime = Integer.parseInt(bannerTime) * 1000;
        }
//        vp_home_banner.setMinimumHeight(screenWidth * (378 / 80));
        //设置banner样式
        vp_home_banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        vp_home_banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        vp_home_banner.setImages(pageList);
        //设置banner动画效果
        vp_home_banner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        vp_home_banner.isAutoPlay(true);
        //设置轮播时间
        vp_home_banner.setDelayTime(bannerIntervalTime);
        //设置指示器位置（当banner模式中有指示器时）
        vp_home_banner.setIndicatorGravity(BannerConfig.CENTER);
        vp_home_banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                bannersBean = pageList.get(position);
                if (!TextUtils.isEmpty(bannersBean.getBannerUrl())) {
                    String web_url = bannersBean.getBannerUrl();
                    if (web_url.contains("fundDetail")) {
                        String url = null;
                        try {
                            url = URLDecoder.decode(web_url, "utf-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        MentorLogger.Log(TAG, "url=" + url);
                        String fundcode;
                        String fundType;
                        String mainTitle;
                        String subTitle;
                        Map<String, String> map = new HashMap<>();
                        String[] params = url.split("\\?")[1].split("&");
                        for (int i = 0; i < params.length; i++) {
                            //System.out.println("params--" + params[i]);
                            String key = params[i].split("=")[0];
                            String value = "";
                            try {
                                value = params[i].split("=")[1];
                            } catch (Exception exception) {
                                MentorLogger.Log(TAG, "value为null");
                            }
                            map.put(key, value);
                        }
                        fundcode = map.get("fundCode");
                        fundType = map.get("fundType");
                        mainTitle = map.get("fundName") + "  " + map.get("fundCode");
                        subTitle = map.get("fundstateDescription");
                        
                        Intent mIntent = new Intent();
                        mIntent.setClass(getActivity(), FundDetailActivity.class);
                        mIntent.putExtra("fundcode", fundcode);
                        mIntent.putExtra("fundType", fundType);
                        mIntent.putExtra("fundname", map.get("fundName"));
                        mIntent.putExtra("mainTitle", mainTitle);
                        mIntent.putExtra("subTitle", subTitle);
                        startActivity(mIntent);
                    } else {
                        String isLogin = bannersBean.getIsLogin();
                        if (activity.mentorApplication.needLogin() && "1".equals(isLogin)) {
                            HostConstant.SKIP_TYPE = "from_fund_banner";
                            ActivityUtils.gotoLogin(activity, hasUser(), false, activity.findUserId());
                        } else {
                            gotoBannerUrlActivity(bannersBean);
                        }
                    }
                } else if (!TextUtils.isEmpty(bannersBean.getContent())) {
                    String isLogin = bannersBean.getIsLogin();
                    if (activity.mentorApplication.needLogin() && "1".equals(isLogin)) {
                        HostConstant.SKIP_TYPE = "from_fund_banner";
                        ActivityUtils.gotoLogin(activity, hasUser(), false, activity.findUserId());
                    } else {
                        gotoBannerUrlActivity(bannersBean);
                    }
                }
            }
        });
        //banner设置方法全部调用完毕时最后调用
        vp_home_banner.start();
    }
    
    private void gotoBannerUrlActivity(BannersBean.DataBean bean) {
        Intent intent = new Intent();
        intent.setClass(activity, BannerUrlActivity.class);
        if (!TextUtils.isEmpty(bean.getBannerUrl())) {
            String params = "?";
            if (!bean.getBannerUrl().contains("?")) {
                params = "?";
            } else {
                if (bean.getBannerUrl().endsWith("?")) {
                    params = "";
                } else {
                    params = "&";
                }
            }
            intent.putExtra("bannerUrl", bean.getBannerUrl() + params);
        }
        intent.putExtra("content", bean.getContent());
        if (!StringUtils.isNullOrEmpty(bean.getBannerTitle())) {
            intent.putExtra("bannerTitle", bean.getBannerTitle() + "");
        }
        if (!StringUtils.isNullOrEmpty(bean.getBannerDescription())) {
            intent.putExtra("bannerDescription", bean.getBannerDescription());
        }
        
        if (!StringUtils.isNullOrEmpty(bean.getBannerPicture())) {
            intent.putExtra("bannerPicture", bean.getBannerPicture());
        }
        activity.startActivity(intent);
    }
    
    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        getInfos();
        getBanners();
    }
    
    private void refreshComplete() {
        if (isBannerRefreshSuccess && isFundRefreshSuccess) {
            pullToRefreshView.onHeaderRefreshComplete();
            
            isFundRefreshSuccess = false;
            isBannerRefreshSuccess = false;
        }
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_fund_home_all: //全部基金
                getActivity().startActivity(new Intent(getActivity(), FundListAllActivity.class));
                break;
            case R.id.ll_fund_home_my_focus:  //我的关注
                break;
            case R.id.ll_fund_home_income_ranking: //收益排行
                getActivity().startActivity(new Intent(getActivity(), FundIncomeRankingActivity.class));
                break;
            default:
                break;
        }
    }
    
    public void checkInit() {
        if (!isFundLoadSuccess) {
            getInfos();
        }
        
        if (!isBannerLoadSuccess) {
            getBanners();
        }
    }
    
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFundBannerLoginCallBack(FundBannerLoginEvent event) {
        if (event.isSuccess) {
            if (bannersBean != null) {
                gotoBannerUrlActivity(bannersBean);
            }
        }
    }
}
